<?php
session_start();
if (! isset($_SESSION["username"])) {
    exit("need_login");
}

if (!isset($_FILES["file"])) {
    exit("file not found");
}
if ($_FILES["file"]["error"] > 0) {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    exit("file error");
}

$id = $_POST["id"];
$name = $_POST["name"];
$extension_name = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
$path = "img/";
$tmp = $_FILES['file']['tmp_name'];
$imgpath = $path.$id.".".$extension_name;
move_uploaded_file($tmp, $imgpath);

include "db.php";
$stmt = $db->prepare("insert into students values(:id, :name, :birth, :tel, :mclass, :imgurl)");
$stmt->bindValue(":id", $id, SQLITE3_TEXT);
$stmt->bindValue(":name", $name, SQLITE3_TEXT);
$stmt->bindValue(":birth", $_POST["TimePicker"], SQLITE3_TEXT);
$stmt->bindValue(":tel", $_POST["tel"], SQLITE3_TEXT);
$stmt->bindValue(":mclass", $_POST["mclass"], SQLITE3_TEXT);
$stmt->bindValue(":imgurl", $imgpath, SQLITE3_TEXT);
if ($stmt->execute()) {
    echo "ok";
} else {
    echo $db->lastErrorMsg();
}


/*
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
*/
?>